//
//  ViewController.m
//  AudioUnitDemo
//
//  Created by ilongge on 2021/9/1.
//

#import "ViewController.h"
#import "AudioRecorder.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;

@property (nonatomic, strong, getter=getRecorder) AudioRecorder *recorder;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getRecorder];
    self.pauseButton.hidden = YES;
    // Do any additional setup after loading the view.
}

- (IBAction)startRecodVoice:(UIButton *)sender {
    [self.recorder startAudioRecord];
    self.pauseButton.hidden = NO;
}
- (IBAction)pauseRecodVoice:(UIButton *)sender {
    if (sender.isSelected == NO) {
        [self.recorder pauseAudioRecord];
    }
    else{
        [self.recorder continueAudioRecord];
    }
    sender.selected = !sender.isSelected;
}
- (IBAction)stopRecodVoice:(UIButton *)sender {
    NSString *recordFilePath = [self.recorder stopAudioRecord];
    NSLog(@"录音文件路径：\n%@", recordFilePath);
    self.pauseButton.hidden = YES;
}

-(AudioRecorder *)getRecorder{
    if (_recorder == nil) {
        AudioRecorderDescription *desc = [[AudioRecorderDescription alloc] init];
        desc.preferredSampleRate = AudioRecorderSampleRate_256000;
        desc.preferredIOBufferDuration = 0.001;
        desc.preferredInputChannels = AudioRecorderChannel_Mono;
        desc.preferredOuputChannels = AudioRecorderChannel_Stereo;
        desc.preferredDiskBufferSize = 256;
        desc.enableDiskBuffer = YES;
        desc.enablePlayWhenRecord = YES;
        _recorder = [[AudioRecorder alloc] initWithPreferredRecorderDescription:desc];
    }
    return _recorder;
}
@end
